package com.white.commonlib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.white.commonlib.R;
import com.white.commonlib.utils.DrawableUtils;

/**
 * Created by yutf on 2018/1/31 0031.
 */

public class RoundRectTextView extends android.support.v7.widget.AppCompatCheckedTextView
{

    private boolean isRadiusWrapHeight;
    private int allRadius;
    private int tlRadius, trRadius, blRadius, brRadius;
    private int activeStyle;

    private int activeColor, normalColor;
    private int activeTxtColor, normalTxtColor;

    private int strokeWidth;

    public static final int STROKE_TO_SOLID = 0;
    public static final int SOLID_TO_SOLID = 1;
    public static final int STROKE_TO_STROKE = 2;

    public RoundRectTextView(Context context)
    {
        this(context, null);
    }

    public RoundRectTextView(Context context, @Nullable AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public RoundRectTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs)
    {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundRectTextView);
        isRadiusWrapHeight = ta.getBoolean(R.styleable.RoundRectTextView_rrtv_round_to_half, false);
        allRadius = ta.getDimensionPixelSize(R.styleable.RoundRectTextView_rrtv_round_radius, 0);
        tlRadius = ta.getDimensionPixelSize(R.styleable.RoundRectTextView_rrtv_round_tl_radius, 0);
        trRadius = ta.getDimensionPixelSize(R.styleable.RoundRectTextView_rrtv_round_tr_radius, 0);
        blRadius = ta.getDimensionPixelSize(R.styleable.RoundRectTextView_rrtv_round_bl_radius, 0);
        brRadius = ta.getDimensionPixelSize(R.styleable.RoundRectTextView_rrtv_round_br_radius, 0);
        activeStyle = ta.getInteger(R.styleable.RoundRectTextView_rrtv_active_style, STROKE_TO_STROKE);

        activeColor = ta.getColor(R.styleable.RoundRectTextView_rrtv_active_color, Color.BLACK);
        normalColor = ta.getColor(R.styleable.RoundRectTextView_rrtv_normal_color, Color.GRAY);

        activeTxtColor = ta.getColor(R.styleable.RoundRectTextView_rrtv_active_txt_color, Color.BLACK);
        normalTxtColor = ta.getColor(R.styleable.RoundRectTextView_rrtv_normal_txt_color, Color.GRAY);

        strokeWidth = ta.getDimensionPixelSize(R.styleable.RoundRectTextView_rrtv_stroke_width, 1);
        ta.recycle();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        config();
    }

    private void config()
    {
        int height = getMeasuredHeight();
        if (isRadiusWrapHeight)
        {
            float finalRadius = height * 1.0f / 2;
            setBgDrawable(finalRadius, finalRadius, finalRadius, finalRadius);
        } else
        {
            if (allRadius != 0)
            {
                setBgDrawable(allRadius, allRadius, allRadius, allRadius);
            } else
            {
                setBgDrawable(tlRadius, trRadius, blRadius, brRadius);
            }
        }
        setTextColor(DrawableUtils.createColorStateList(activeTxtColor, normalTxtColor));
    }

    private void setBgDrawable(float tlr, float trr, float blr, float brr)
    {
        switch (activeStyle)
        {
            case STROKE_TO_SOLID:
                Drawable active1 = DrawableUtils.createRoundRectShape(tlr, trr, blr, brr,
                        activeColor, false, 0);
                Drawable normal1 = DrawableUtils.createRoundRectShape(tlr, trr, blr, brr,
                        normalColor, true, strokeWidth);
                setBackground(DrawableUtils.createSelectorDrawable(active1, normal1));
                break;
            case STROKE_TO_STROKE:
                Drawable active2 = DrawableUtils.createRoundRectShape(tlr, trr, blr, brr,
                        activeColor, true, strokeWidth);
                Drawable normal2 = DrawableUtils.createRoundRectShape(tlr, trr, blr, brr,
                        normalColor, true, strokeWidth);
                setBackground(DrawableUtils.createSelectorDrawable(active2, normal2));
                break;
            case SOLID_TO_SOLID:
                Drawable active3 = DrawableUtils.createRoundRectShape(tlr, trr, blr, brr,
                        activeColor, false, 0);
                Drawable normal3 = DrawableUtils.createRoundRectShape(tlr, trr, blr, brr,
                        normalColor, false, 0);
                setBackground(DrawableUtils.createSelectorDrawable(active3, normal3));
                break;
        }
    }
}
